package cn.rkylin.oms.common.report.controller;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.rkylin.core.controller.ApolloController;
import cn.rkylin.oms.common.context.CurrentUser;
import cn.rkylin.oms.common.context.WebContextFactory;

/**
 * 报表控制器
 *
 * @created 2018-1-15 14:35:03
 */
@Controller
@RequestMapping("/report")
public class ReportController extends ApolloController {
    private static final Log logger = LogFactory.getLog(ReportController.class);

    // 报表服务器地址(应如：http://localhost:8080/jasperserver/)
    @Value("${report.server.url}")
    private String reportServerUrl = "http://localhost:8080/jasperserver/";

    // 报表HTML定义
    private static final Map<String, String> REPORT_PAGE ; // key:报表名，value:HTML路径
    static {
    	REPORT_PAGE = new HashMap<String, String>();
    	REPORT_PAGE.put("ORDER_REPORT", "redirect:/report.html");
    	REPORT_PAGE.put("ITEM_TURNOVER_REPORT", "redirect:/views/report/itemTurnoverReport.html");
    	REPORT_PAGE.put("ITEM_SALE_REPORT", "redirect:/views/report/itemSaleReport.html");
        // ......其它报表key,value
    }
    
    // 报表路径定义(value请见JasperReport Server的报表属性中的路径)
    private static final Map<String, String> REPORT_MAP; // key:报表名，value:报表路径
    static {
        REPORT_MAP = new HashMap<String, String>();
        REPORT_MAP.put("ORDER_REPORT", "/reports/order/OrderReport_XVP");
        REPORT_MAP.put("ITEM_TURNOVER_REPORT", "/reports/Item_turnover");
        REPORT_MAP.put("ITEM_SALE_REPORT", "/reports/");
        // ......其它报表key,value
    }

    /**
     * 跳转到店铺列表
     */
    @RequestMapping(value = "/show")
    public String showReport(String reportName, String shopId) {
        CurrentUser currUser = WebContextFactory.getWebContext().getCurrentUser();
        // 没有登录或者没有参数不能转到报表
        if (StringUtils.isNotBlank(reportName) && StringUtils.isNotBlank(shopId) && currUser != null) {
            String reportURL = reportServerUrl + "flow.html?_flowId=viewReportFlow&decorate=no&reportUnit=" + REPORT_MAP.get(reportName) + "&p_shopId=" + shopId;
            String targetURL = REPORT_PAGE.get(reportName) + "?reportURL=" + reportURL;//URLEncoder.encode(reportURL);
            return targetURL;
        }
        return REPORT_PAGE.get(reportName);
    }
    
    /**
     * 跳转到报表列表
     */
    @RequestMapping(value = "/showReport")
    public String showReportData(String reportName) {
        CurrentUser currUser = WebContextFactory.getWebContext().getCurrentUser();
        // 没有登录或者没有参数不能转到报表
        if (StringUtils.isNotBlank(reportName) && currUser != null) {
            String reportURL = reportServerUrl + "flow.html?_flowId=viewReportFlow&decorate=no&reportUnit=" + REPORT_MAP.get(reportName);
            String targetURL = REPORT_PAGE.get(reportName) + "?reportURL=" + reportURL;//URLEncoder.encode(reportURL);
            return targetURL;
        }
        return REPORT_PAGE.get(reportName);
    }
    
}
